export default {
	myRequest(obj) {
		return new Promise((resolve, reject) => {
			uni.request({
				// #ifndef  H5
				url: 'http://localhost:3000' + (obj.url ? obj.url : ''),
				// #endif

				// #ifdef  H5
				url: '/api' + (obj.url ? obj.url : ''),
				// #endif
				data: obj.data || {},
				header: obj.header || {},
				method: obj.method || 'POST',
				success: (res) => {
					resolve(res.data)
				},
				fail(err) {
					reject(err)
				}
			});
		})

	},
	myConnectSocket() {
		let {
			uuid
		} = uni.getStorageSync('user')
		uni.connectSocket({
			url: 'ws://localhost:8080/' + uuid,
			header: {
				'content-type': 'application/json'
			},
			protocols: ['echo-protocol'],
			method: 'GET'
		})
		uni.onSocketOpen(res => {
			console.log('WebSocket连接已打开！');
			this.$store.commit('socketState', true)
		});
		uni.onSocketClose(res => {
			console.log('WebSocket 已关闭！');
			this.$store.commit('socketState', false)
		});
		// 注：只有连接正常打开中 ，才能正常收到消息
		uni.onSocketMessage(res => {
			console.log('onSocketMessage')
			this.$store.commit('unreadSumAdd')
		});

	},
	myUploadFile(obj) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				// #ifndef  H5
				url: 'http://localhost:8000/api/uploadFile',
				// #endif

				// #ifdef  H5
				url: '/api/uploadFile',
				// #endif
				filePath: obj.tempFilePath,
				name: 'file',
				formData: obj.data || {},
				success: (res) => {
					resolve(res.data)
				},
				fail(err) {
					reject(err)
				}
			});
		})

	}
}
